package com.example.noobpackage.ui.packagemarketdetail

import android.os.Bundle
import android.transition.TransitionInflater
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.MarketPackageData
import com.example.noobpackage.logic.repository.MarketRepository
import com.example.noobpackage.tool.ExpressIconUtil
import com.example.noobpackage.ui.MainViewModel
import com.example.noobpackage.ui.packagemarket.PackageMarketFragmentDirections
import com.example.noobpackage.ui.packagemarket.PackageMarketViewModel
import kotlinx.android.synthetic.main.fragment_package_market_detail.*
import kotlin.concurrent.thread

class PackageMarketDetailFragment : Fragment() {
    private val args: PackageMarketDetailFragmentArgs by navArgs()
    private lateinit var packageData: MarketPackageData
    private val sharedViewModel: MainViewModel by activityViewModels()
    private val viewModel: PackageMarketDetailViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition =
            TransitionInflater.from(context).inflateTransition(android.R.transition.move)
                .setDuration(400)

        // 加载初始数据
        packageData = sharedViewModel.getMarketPackage(args.position)!!
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_package_market_detail, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        // 图片动态绑定
        typeImage.setImageResource(ExpressIconUtil.getIconResource(packageData.expressType))

        // 过渡动画绑定
        super.onViewCreated(view, savedInstanceState)
        detailTakingPlaceContent.transitionName = "takingPlace${args.position}"
        detailTakingToPlaceContent.transitionName = "takingToPlace${args.position}"
        detailWeightContent.transitionName = "weight${args.position}"
        detailRemarksContent.transitionName = "remarks${args.position}"
        detailTakingPlaceIcon.transitionName = "takingPlaceIcon${args.position}"
        detailTakingToPlaceIcon.transitionName = "takingToPlaceIcon${args.position}"
        detailWeightIcon.transitionName = "weightIcon${args.position}"
        detailRemarksIcon.transitionName = "remarksIcon${args.position}"

        // 数据绑定
        packageData.let {
            typeContent.text = it.expressType
            detailTakingPlaceContent.text = it.expressLocation
            detailTakingToPlaceContent.text = it.expressToLocation
            detailWeightContent.text = it.expressWeight
            detailRemarksContent.text = it.expressRemarks
        }

        // 接收请求点击事件
        acceptButton.setOnClickListener {
            // Loading动画
            acceptButton.visibility = View.INVISIBLE
            progressInfo.visibility = View.VISIBLE
            val account = sharedViewModel.userInfo.value?.account!!
            viewModel.acceptMarketPackage(packageData.marketId, account)
                .observe(viewLifecycleOwner, {
                    when (it.isSuccess) {
                        true -> {
                            // 成功动画
                            progressInfo.visibility = View.INVISIBLE
                            successInfo.visibility = View.VISIBLE
                            // 跳转到前一个页面，同时刷新列表
                            // 会重走上一个界面的onCreateView和onViewCreated生命周期方法
                            // 不知道能否正确把已接受的刷新掉，不然手快点又能点进来
//                            findNavController().navigateUp()
                        }
                        false -> {
                            progressInfo.visibility = View.INVISIBLE
                            acceptButton.visibility = View.VISIBLE
                            Toast.makeText(context, "接受失败", Toast.LENGTH_SHORT).show()
                        }
                    }
                })
        }

    }

}